/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.srcptit.trainning.thompv.BaitapMuc1_2;
import java.util.Scanner;
/**
 *
 * @author SONY
 */
public class Bai4Muc2 {
    static int k,n,ok=1;
    static int []a=new int[100];
    public static void Init()
    {
        for(int i=1;i<=n;i++)
            a[i]=i;
    }
    public static void in()
    {
        for(int i=1;i<=k;i++)
            System.out.print(a[i]+"  ");
        System.out.println("");
    }
    public static void sinh()
    {
        int i=k;
        while(i>0&&a[i]==n-k+i) i--;
        if(i>0)
        {
            a[i]=a[i]+1;
            for(int j=i+1;j<=k;j++)
                a[j]=a[i]+j-i;
        }
        else ok=0;
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int stop=0;
        String s;
        System.out.print("Nhap n = ");
        while(stop==0)
        {
            try
            {
                s=sc.nextLine();
                if(s.length()<1) throw new Exception("");
                n=Integer.parseInt(s);
                if(n<=0) throw new Exception("");
                System.out.print("Nhap so luong K phan tu:");
                int stop1=0;
                String s1;
                while(stop1==0)
                {
                    try
                    {
                        s1=sc.nextLine();
                        if(s1.length()<1) throw new Exception("");
                        k=Integer.parseInt(s1);
                        if(k<=0) throw new Exception("");
                        stop1=1;
                    }
                    catch (Exception e)
                    {
                        System.out.println("Nhap lai K");
                    }
                }
                stop=1;
                Init();
                System.out.println("Ket qua:");
                while(ok==1)
                {
                    in();
                    sinh();
                }
            }
            catch (Exception e)
            {
                System.out.println("Nhap lai so luong phan tu N");
            }
        }
    }
}
